/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShBuildingDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShBuildingService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShBuilding;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * ShBuildingService 实现类
 * 
 * @author KongXiangyu
 * @since 2015-09-07
 */
@Service("shBuildingService")
public class ShBuildingServiceImpl extends BaseServiceImpl<ShBuilding, Long>
		implements ShBuildingService {

	@Resource
	private ShBuildingDao shBuildingDao;

	public BaseDao<ShBuilding, Long> getDao() {
		return shBuildingDao;
	}


	@Override
	public DataGrid getDataGrid(ShBuilding building, PageHelper page) {
		DataGrid grid = new DataGrid();

		if(building == null) building = new ShBuilding();
		if (!StringUtils.isBlank(page.getOrder())) {
			building.setOrderFieldType(page.getOrder());
		}
		if (!StringUtils.isBlank(page.getSort())) {
			building.setOrderField(page.getSort());
		}
		int count = shBuildingDao.selectEntryListCount(building);
		grid.setTotal(count + 0L);
		if(count > 0){
			int startIndex = (page.getPage() - 1) * page.getRows();
			int endIndex = page.getPage() * page.getRows();
			building.setStartIndex(startIndex);
			building.setEndIndex(endIndex);
			grid.setRows(shBuildingDao.seleteBuildings(building));
		}
		
		return  grid;
	}

	@Override
	public DataGrid getUnitDataGrid(ShBuilding building, PageHelper page) {
		DataGrid grid = new DataGrid();
		
		if (!StringUtils.isBlank(page.getOrder())) {
			building.setOrderFieldType(page.getOrder());
		}
		if (!StringUtils.isBlank(page.getSort())) {
			building.setOrderField(page.getSort());
		}
		
		int count = shBuildingDao.selectEntryListCount(building);
		grid.setTotal(count + 0L);
		if(count > 0){
			int startIndex = (page.getPage() - 1) * page.getRows();
			int endIndex = page.getPage() * page.getRows();
			building.setStartIndex(startIndex);
			building.setEndIndex(endIndex);
			grid.setRows(shBuildingDao.seleteBuidingUnits(building));
		}
		
		return  grid;
		
	}
}